package com.my.roco.controller;

import com.my.roco.pojo.Skill;
import com.my.roco.service.SkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import javax.servlet.http.HttpSession;

@Controller
public class SkillController {

    @Autowired
    private SkillService skillService;

    @GetMapping("/addSkillPage")
    public ModelAndView showAddSkillPage(@RequestParam("petNumber") String petNumber) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("petNumber", petNumber);
        mv.setViewName("addSkill");
        return mv;
    }

    @PostMapping("/addSkill")
    public String addSkill(@RequestParam("petNumber") String petNumber,
                           @RequestParam("name") String name,
                           @RequestParam("number") String number,
                           @RequestParam("power") int power,
                           @RequestParam("pp") int pp,
                           @RequestParam("introduction") String introduction,
                           HttpSession session) {
        Skill skill = new Skill();
        skill.setPetNumber(petNumber);
        skill.setName(name);
        skill.setNumber(number);
        skill.setPower(power);
        skill.setPp(pp);
        skill.setIntroduction(introduction);

        String providerUsername = (String) session.getAttribute("username");
        skill.setProviderUsername(providerUsername);

        skillService.addSkill(skill);
        return "redirect:/petDetail?number=" + petNumber;
    }
    
    @GetMapping("/deleteSkill")
    public String deleteSkill(@RequestParam("id") int id, @RequestParam("petNumber") String petNumber) {
        skillService.deleteSkill(id);
        return "redirect:/petDetail?number=" + petNumber;
    }
}